package com.wudl.core;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.Arrays;
import java.util.List;

/**
 * @version v1.0
 * @ProjectName Flinklearning
 * @ClassName TransformFlatMap
 * @Description TODO FlatMap
 *
 * FlatMap: 是一种扁平的映射，将数据流中的整体拆分成为一个个的个体使用， 消费后的元素产生零到多个元素
 *
 *
 *
 * @Author wudl
 * @Date 2020/10/29 10:46
 *
 *
 * 函数 FlatMap
 * 将数据流中的整体拆分成一个 一个 的个体使用， 消费一个元素并产生零到多个元素
 *参数： lambda 表达式或者是FlatFunction的实现类
 * 返回值：DataStream
 *
 *
 *
 */

public class TransformFlatMap {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
//        DataStreamSource<List<Integer>> listDs = env.fromCollection(Arrays.asList(
//                Arrays.asList(1, 2, 3),
//                Arrays.asList(3, 4, 5),
//                Arrays.asList(8,9,0)
//        ));




//        listDs.flatMap(new FlatMapFunction<List<Integer>, Integer>() {
//            @Override
//            public void flatMap(List<Integer> list, Collector<Integer> collector) throws Exception {
//
//                for (Integer number : list) {
//                    collector.collect(number + 100);
//                }
//
//            }
//        }).print();



        DataStreamSource<String> strDs = env.socketTextStream("10.204.125.140", 8899);
        strDs.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String s, Collector<String> collector) throws Exception {
                String[] split = s.split(",");
                collector.collect(split[0]+split[1]);
            }
        }).print();








        env.execute();

    }

}
